//
//  RCMerchantPictureCell.m
//  RocketProject
//
//  Created by Wu Chengwei on 13-9-29.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCMerchantPictureCell.h"
#import "UIColor-Expanded.h"
#import "CommonDefines.h"
#import "RCMerchantPicturesDataModel.h"
#import "UIImageView+WebCache.h"
#import "NSString+NSString_Ex_.h"

#define kDefaultimageFile        @"image_recommendFood_image_bg.png"
#define kStartActiveImageFile    @"image_start_active.png"
#define kStartInActiveImageFile  @"image_merchant_inactive.png"

@interface RCMerchantPictureCell()

@property(nonatomic,retain)UIImageView *defaultLeftImageView;
@property(nonatomic,retain)UIImageView *picLeftImageView;
@property(nonatomic,retain)UIButton *picLeftTransparentButton;
@property(nonatomic,retain)UILabel *leftTitelLabel;
@property(nonatomic,retain)UIView *leftStarView;
@property(nonatomic,retain)UIImageView *startLeftImageView1;
@property(nonatomic,retain)UIImageView *startLeftImageView2;
@property(nonatomic,retain)UIImageView *startLeftImageView3;
@property(nonatomic,retain)UIImageView *startLeftImageView4;
@property(nonatomic,retain)UIImageView *startLeftImageView5;
@property(nonatomic,retain)UILabel *leftPriceLabel;

@property(nonatomic,retain)UIImageView *defaultRightImageView;
@property(nonatomic,retain)UIImageView *picRightImageView;
@property(nonatomic,retain)UIButton *picRightTransparentButton;
@property(nonatomic,retain)UILabel *rightTitelLabel;
@property(nonatomic,retain)UIView *rightStarView;
@property(nonatomic,retain)UIImageView *startRightImageView1;
@property(nonatomic,retain)UIImageView *startRightImageView2;
@property(nonatomic,retain)UIImageView *startRightImageView3;
@property(nonatomic,retain)UIImageView *startRightImageView4;
@property(nonatomic,retain)UIImageView *startRightImageView5;
@property(nonatomic,retain)UILabel *rightPriceLabel;


@end

@implementation RCMerchantPictureCell

@synthesize delegate = _delegate;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        self.contentView.backgroundColor = [UIColor clearColor];
        self.backgroundColor = [UIColor clearColor];
        [self createPlaceHolderView];
    }
    return self;
}

-(void)dealloc{
    self.defaultLeftImageView = nil;
    self.picLeftImageView = nil;
    self.picLeftTransparentButton = nil;
    self.leftTitelLabel = nil;
    self.leftStarView = nil;
    self.startLeftImageView1 = nil;
    self.startLeftImageView2 = nil;
    self.startLeftImageView3 = nil;
    self.startLeftImageView4 = nil;
    self.startLeftImageView5 = nil;
    self.leftPriceLabel = nil;
    
    self.defaultRightImageView = nil;
    self.picRightImageView = nil;
    self.picRightTransparentButton = nil;
    self.rightTitelLabel = nil;
    self.rightStarView = nil;
    self.startRightImageView1 = nil;
    self.startRightImageView2 = nil;
    self.startRightImageView3 = nil;
    self.startRightImageView4 = nil;
    self.startRightImageView5 = nil;
    self.rightPriceLabel = nil;
    self.delegate = nil;
    
    [super dealloc];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(void)createPlaceHolderView{
    _defaultLeftImageView = [[UIImageView alloc]initWithFrame:CGRectMake(17, 5, 130, 83)];
    _defaultLeftImageView.backgroundColor = [UIColor clearColor];
    UIImage *defaultImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kDefaultimageFile ofType:nil]];
    defaultImage = [defaultImage resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
    [_defaultLeftImageView setImage:defaultImage];
    [self addSubview:_defaultLeftImageView];
    
    _picLeftImageView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 8, 124, 77)];
    _picLeftImageView.backgroundColor = [UIColor clearColor];
    _picLeftImageView.userInteractionEnabled = YES;
    [self addSubview:_picLeftImageView];
    
    _picLeftTransparentButton = [[UIButton alloc]initWithFrame:_defaultLeftImageView.bounds];
    _picLeftTransparentButton.backgroundColor = [UIColor clearColor];
    [_picLeftTransparentButton addTarget:self action:@selector(transparentButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [_picLeftImageView addSubview:_picLeftTransparentButton];
    
    
    
    _leftTitelLabel = [[UILabel alloc]initWithFrame:CGRectMake(17, 90, 130, 16)];
    _leftTitelLabel.backgroundColor = [UIColor clearColor];
    _leftTitelLabel.textColor = [UIColor colorWithHex:0xff505050];
    _leftTitelLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:16];
    [self addSubview:_leftTitelLabel];
    
    _leftStarView = [[UIView alloc]initWithFrame:CGRectMake(17, 111, 75, 14)];
    _leftStarView.backgroundColor = [UIColor clearColor];
    
    
    _startLeftImageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 14, 14)];
    _startLeftImageView1.backgroundColor = [UIColor clearColor];
    [_leftStarView addSubview:_startLeftImageView1];
    
    _startLeftImageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(15, 0, 14, 14)];
    _startLeftImageView2.backgroundColor = [UIColor clearColor];
    [_leftStarView addSubview:_startLeftImageView2];
    
    _startLeftImageView3 = [[UIImageView alloc]initWithFrame:CGRectMake(30, 0, 14, 14)];
    _startLeftImageView3.backgroundColor = [UIColor clearColor];
    [_leftStarView addSubview:_startLeftImageView3];
    
    _startLeftImageView4 = [[UIImageView alloc]initWithFrame:CGRectMake(45, 0, 14, 14)];
    _startLeftImageView4.backgroundColor = [UIColor clearColor];
    [_leftStarView addSubview:_startLeftImageView4];
    
    _startLeftImageView5 = [[UIImageView alloc]initWithFrame:CGRectMake(60, 0, 14, 14)];
    _startLeftImageView5.backgroundColor = [UIColor clearColor];
    [_leftStarView addSubview:_startLeftImageView5];
    
    [self addSubview:_leftStarView];
    
    _leftPriceLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 113, 47, 12)];
    _leftPriceLabel.backgroundColor = [UIColor clearColor];
    _leftPriceLabel.textColor = [UIColor colorWithHex:0xff9897997];
    _leftPriceLabel.font = [UIFont fontWithName:kFontNameHelvetica size:12];
    [self addSubview:_leftPriceLabel];
    
    
    _defaultRightImageView = [[UIImageView alloc]initWithFrame:CGRectMake(170, 5, 130, 83)];
    _defaultRightImageView.backgroundColor = [UIColor clearColor];
    UIImage *defaultRightImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kDefaultimageFile ofType:nil]];
    defaultRightImage = [defaultRightImage resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
    [_defaultRightImageView setImage:defaultRightImage];
    [self addSubview:_defaultRightImageView];
    
    _picRightImageView = [[UIImageView alloc]initWithFrame:CGRectMake(173, 8, 124, 77)];
    _picRightImageView.backgroundColor = [UIColor clearColor];
    _picRightImageView.userInteractionEnabled = YES;
    [self addSubview:_picRightImageView];
    
    _picRightTransparentButton = [[UIButton alloc]initWithFrame:_defaultRightImageView.bounds];
    _picRightTransparentButton.backgroundColor = [UIColor clearColor];
    [_picRightTransparentButton addTarget:self action:@selector(transparentButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [_picRightImageView addSubview:_picRightTransparentButton];
    
    _rightTitelLabel = [[UILabel alloc]initWithFrame:CGRectMake(170, 90, 130, 16)];
    _rightTitelLabel.backgroundColor = [UIColor clearColor];
    _rightTitelLabel.textColor = [UIColor colorWithHex:0xff505050];
    _rightTitelLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:16];
    [self addSubview:_rightTitelLabel];
    
    _rightStarView = [[UIView alloc]initWithFrame:CGRectMake(170, 111, 75, 14)];
    _rightStarView.backgroundColor = [UIColor clearColor];
    
    
    _startRightImageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 14, 14)];
    _startRightImageView1.backgroundColor = [UIColor clearColor];
    [_rightStarView addSubview:_startRightImageView1];
    
    _startRightImageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(15, 0, 14, 14)];
    _startRightImageView2.backgroundColor = [UIColor clearColor];
    [_rightStarView addSubview:_startRightImageView2];
    
    _startRightImageView3 = [[UIImageView alloc]initWithFrame:CGRectMake(30, 0, 14, 14)];
    _startRightImageView3.backgroundColor = [UIColor clearColor];
    [_rightStarView addSubview:_startRightImageView3];
    
    _startRightImageView4 = [[UIImageView alloc]initWithFrame:CGRectMake(45, 0, 14, 14)];
    _startRightImageView4.backgroundColor = [UIColor clearColor];
    [_rightStarView addSubview:_startRightImageView4];
    
    _startRightImageView5 = [[UIImageView alloc]initWithFrame:CGRectMake(60, 0, 14, 14)];
    _startRightImageView5.backgroundColor = [UIColor clearColor];
    [_rightStarView addSubview:_startRightImageView5];
    
    [self addSubview:_rightStarView];
    
    _rightPriceLabel = [[UILabel alloc]initWithFrame:CGRectMake(253, 113, 47, 12)];
    _rightPriceLabel.backgroundColor = [UIColor clearColor];
    _rightPriceLabel.textColor = [UIColor colorWithHex:0xff9897997];
    _rightPriceLabel.font = [UIFont fontWithName:kFontNameHelvetica size:12];
    [self addSubview:_rightPriceLabel];
}

-(void)bindData:(RCMerchantPictureItem *)firstItem withFirstTag:(NSInteger)firstTag withSecondItem:(RCMerchantPictureItem *)secondItem withSecondTag:(NSInteger)secondTag{
    if (nil == firstItem && nil == secondItem) {
        return;
    }
    
    _picLeftTransparentButton.hidden = YES;
    if (![NSString stringIsNullOrEmpty:firstItem.photoPicUrl]) {
        [_picLeftImageView setImageWithURL:[NSURL URLWithString:firstItem.photoPicUrl] placeholderImage:nil];
        _picLeftTransparentButton.hidden = NO;
    }
    _picLeftTransparentButton.tag = firstTag;
    
    _leftTitelLabel.hidden = YES;
    if (![NSString stringIsNullOrEmpty:firstItem.photoTitle]) {
        _leftTitelLabel.hidden = NO;
        _leftTitelLabel.text = firstItem.photoTitle;
    }
    
    _leftStarView.hidden = YES;
    if (firstItem.photoScore > 0) {
        _leftStarView.hidden = NO;
        NSInteger score = firstItem.photoScore;
        NSString *resourcePath = [[NSBundle mainBundle]resourcePath];
        UIImage *activeImage = [UIImage imageWithContentsOfFile:[resourcePath stringByAppendingPathComponent:kStartActiveImageFile]];
        UIImage *inactiveImage = [UIImage imageWithContentsOfFile:[resourcePath stringByAppendingPathComponent:kStartInActiveImageFile]];
        [_startLeftImageView1 setImage:score > 0 ? activeImage : inactiveImage];
        [_startLeftImageView2 setImage:score > 1 ? activeImage : inactiveImage];
        [_startLeftImageView3 setImage:score > 2 ? activeImage : inactiveImage];
        [_startLeftImageView4 setImage:score > 3 ? activeImage : inactiveImage];
        [_startLeftImageView5 setImage:score > 4 ? activeImage : inactiveImage];
    }
    
    _leftPriceLabel.hidden = YES;
    if (firstItem.photoPrice > 0) {
        _leftPriceLabel.hidden = NO;
        _leftPriceLabel.text = [NSString stringWithFormat:@"￥%d",firstItem.photoPrice];
    }
    
    if (nil == secondItem) {
        _defaultRightImageView.hidden = YES;
        _picRightImageView.hidden = YES;
        _picRightTransparentButton.hidden = YES;
        _rightTitelLabel.hidden = YES;
        _rightStarView.hidden = YES;
        _rightPriceLabel.hidden = YES;
        return;
    }
    
    _defaultRightImageView.hidden = NO;
    _picRightImageView.hidden = NO;

    
    _picRightTransparentButton.hidden = YES;
    if (![NSString stringIsNullOrEmpty:secondItem.photoPicUrl]) {
        [_picRightImageView setImageWithURL:[NSURL URLWithString:secondItem.photoPicUrl] placeholderImage:nil];
        _picRightTransparentButton.hidden = NO;
    }
    _picRightTransparentButton.tag = secondTag;
    
    _rightTitelLabel.hidden = YES;
    if (![NSString stringIsNullOrEmpty:secondItem.photoTitle]) {
        _rightTitelLabel.hidden = NO;
        _rightTitelLabel.text = secondItem.photoTitle;
    }
    
    _rightStarView.hidden = YES;
    if (secondItem.photoScore > 0) {
        _rightStarView.hidden = NO;
        NSInteger score = secondItem.photoScore;
        NSString *resourcePath = [[NSBundle mainBundle]resourcePath];
        UIImage *activeImage = [UIImage imageWithContentsOfFile:[resourcePath stringByAppendingPathComponent:kStartActiveImageFile]];
        UIImage *inactiveImage = [UIImage imageWithContentsOfFile:[resourcePath stringByAppendingPathComponent:kStartInActiveImageFile]];
        [_startRightImageView1 setImage:score > 0 ? activeImage : inactiveImage];
        [_startRightImageView2 setImage:score > 1 ? activeImage : inactiveImage];
        [_startRightImageView3 setImage:score > 2 ? activeImage : inactiveImage];
        [_startRightImageView4 setImage:score > 3 ? activeImage : inactiveImage];
        [_startRightImageView5 setImage:score > 4 ? activeImage : inactiveImage];
    }
    
    _rightPriceLabel.hidden = YES;
    if (secondItem.photoPrice > 0) {
        _rightPriceLabel.hidden = NO;
        _rightPriceLabel.text = [NSString stringWithFormat:@"￥%d",secondItem.photoPrice];
    }
}

-(void)transparentButtonClicked:(id)sender{
    UIButton *button = (UIButton *)sender;
    UIImageView *imageView = (UIImageView *)button.superview;
    if (_delegate && [_delegate respondsToSelector:@selector(gotoChildViewWithTag:withImageView:)]) {
        [_delegate gotoChildViewWithTag:button.tag withImageView:imageView];
    }
}

@end
